package com.skplanet.oneplusonewindow.moimserver.service;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skplanet.oneplusonewindow.moimserver.dao.BoardDAO;
import com.skplanet.oneplusonewindow.moimserver.dao.CommentDAO;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Board;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Comment;



@Service(value="boardService")
@Transactional
public class BoardService {
	@Autowired
	private BoardDAO boardDAO;
	@Autowired
	private CommentDAO commentDAO;
	
	@Transactional(readOnly = true)
	public Moim_Board getBoard(long id){
		Moim_Board board = boardDAO.findUniqByObject("id", id);
		return board;
	}
	
	@Transactional
	public void updateReadCount(long id){
		Moim_Board board = boardDAO.findUniqByObject("id", id);
		board.setReadcount(board.getReadcount() + 1);
	}
	
	@Transactional(readOnly = true)
	public int getBoardCount(){
		return boardDAO.getAll().size();
	}
	
	@Transactional(readOnly = true)
	public List<Moim_Board> getBoardList(int pg){
		return boardDAO.getBoardList(pg);
	}
	
	@Transactional
	public void addBoard(Moim_Board board){
		boardDAO.save(board);
	}
	
	@Transactional
	public void updateBoard(Moim_Board board){
		Moim_Board b1 = boardDAO.findUniqByObject("id", board.getId());
		b1.setName(board.getName());
		b1.setContent(board.getContent());
		b1.setTitle(board.getTitle());
	}
	

	@Transactional
	public void deleteBoard(Moim_Board board){
		boardDAO.delete(board);
	}	
	
	@Transactional
	public void addComment(long board_id, Moim_Comment comment){
		Moim_Board board = getBoard(board_id);
		comment.setBoard(board);
		board.addComment(comment);
	}
}
